/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.NiudanAccount;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.activity.NDNiudanActivity;
import com.simon.mengkou.ui.adapter.NDCouponsAdapter;
import com.simon.mengkou.ui.base.BaseNiudanFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/21.
 * desc    : 扭券库
 */
public class NDCouponFragment extends BaseNiudanFragment {
    @Bind(R.id.coupon_id_list) ListView mLvCoupons;

    private NDCouponsAdapter mAdapter;

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_coupon);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());
        setDoorVisible(true);
        openDoor(null, 500);

        //头部
        View viewHeader = LayoutInflater.from(mActivity).inflate(R.layout.layout_coupon_list_header, null);
        mLvCoupons.addHeaderView(viewHeader);

        mAdapter = new NDCouponsAdapter(mActivity);
        mLvCoupons.setAdapter(mAdapter);

        NiudanAccount account = OuerApplication.mUser.getNiudanAccount();
        if(account == null) {
            getNiudanAccount();
        } else {
            mAdapter.setList(account.getCoupons());
        }

    }

    /**
     * 获取我的扭券库
     */
    private void getNiudanAccount() {
        AgnettyFuture future = OuerApplication.mOuerFuture.getNiudanAccount(
                OuerApplication.mUser.getUserId(),
                new OuerFutureListener(mActivity) {
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        NiudanAccount account = (NiudanAccount) result.getAttach();
                        OuerApplication.mUser.setNiudanAccount(account);
                        if(account != null) {
                            mAdapter.setList(account.getCoupons());
                        }
                    }
                });

        attachDestroyFutures(future);
    }


    @Override
    public void onBackPressed() {
        final FragmentManager fm = mActivity.getSupportFragmentManager();
        closeDoor(new NDNiudanActivity.OnDoorCloseListener() {
            @Override
            public void onClose() {
                fm.popBackStack();
                openDoor(null);
            }
        });
    }
}
